#!/usr/bin/python
# -*- coding: utf-8 -*-

# @File        :   task_init.py
# @Version     :   1.0
# @Author      :   zhfnss
# @Email       :   mklv2005@163.com
# @Time        :   2021/03/30 10:40:37
# Description  :   


from datetime import datetime, timedelta
from functools import reduce

import pandas as pd

from model.weather_city import WeatherCity
from utils.db_util import db


def remove_list_dict_duplicate(list_dict_data):
    """ 对list格式的dict进行去重"""
    run_function = lambda x, y: x if y in x else x + [y]
    return reduce(run_function, [[], ] + list_dict_data)


def get_city_task_list():
    """待爬取天气的城市清单"""
    rs = db.session.query(
        WeatherCity.city_id, WeatherCity.city_name
    ).filter(
        WeatherCity.flag == 1
    ).all()
    return [{'city_id': r.city_id, 'city_name': r.city_name} for r in rs]


def get_his_task_list():
    """待爬取历史天气的城市清单"""
    l = []
    rs = db.session.query(
        WeatherCity.city_id,
        WeatherCity.city_name,
        WeatherCity.last_synch_date
    ).filter(
        WeatherCity.flag == 1
    ).all()
    for r in rs:
        # 生成月份清单
        ml = pd.date_range(start=r.last_synch_date, end=(datetime.now() + timedelta(days=-1)), freq='M')
        for d in ml:
            l.append(
                {
                    'city_id': r.city_id,
                    'city_name': r.city_name,
                    'year': d.year,
                    'month': d.month
                }
            )
        # 添加当月
        l.append(
            {
                'city_id': r.city_id,
                'city_name': r.city_name,
                'year': datetime.now().year,
                'month': datetime.now().month
            }
        )
    return remove_list_dict_duplicate(l)
